//
//  SYEmoticonTools.swift
//  YANAWeibo
//
//  Created by mac on 16/5/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SYEmoticonTools: NSObject {
    /// 表情单利全局访问点
    static let shareEmoticonTools: SYEmoticonTools = SYEmoticonTools()
    
    private override init() {
        super.init()
    }
    
    private lazy var emoticonBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        let bundle = NSBundle(path: path)!
        return bundle
    }()
    
    /// 懒加载默认表情
    private lazy var defaultEmoticons: [SYEmoticon] = {
        return self.loadEmoticonsWithPath("default/info.plist")
    }()
    
    /// 懒加载emoji表情
    private lazy var emojiEmoticons: [SYEmoticon] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
    
    /// 懒加载lxh表情
    private lazy var lxhEmoticons: [SYEmoticon] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    
    /// 返回全部模型的三维数组
    lazy var allEmoticons: [[[SYEmoticon]]] = {
        return [
            self.sectionWithEmoticons(self.defaultEmoticons),
            self.sectionWithEmoticons(self.emojiEmoticons),
            self.sectionWithEmoticons(self.lxhEmoticons)
        ]
    }()
    
    // 返回一维模型数组
    private func loadEmoticonsWithPath(subPath: String) -> [SYEmoticon] {
        let path = emoticonBundle.pathForResource(subPath, ofType: nil)!
        let array = NSArray(contentsOfFile: path)!
        
        var tempArray = [SYEmoticon]()
        for dic in array {
            // 字典转模型
            let emoticon = SYEmoticon(dict: dic as! [String : AnyObject])
            // 判断是否是图片
            if emoticon.type == "0" {
                let pngName = emoticon.png!
                let subPath = (path as NSString).stringByDeletingLastPathComponent
                emoticon.path = subPath + "/" + pngName
            }
            tempArray.append(emoticon)
        }
        return tempArray
    }
    
    // 返回二维数组
    private func sectionWithEmoticons(emoticons: [SYEmoticon]) -> [[SYEmoticon]] {
        let pageCount = (emoticons.count - 1) / 20 + 1
        var tempArr = [[SYEmoticon]]()
        for i in 0..<pageCount {
            // 计算当前截取的索引
            let location = i * 20
            var length = 20
            if location + length > emoticons.count {
                length = emoticons.count - location
            }
            // 截取数据
            let subArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(location, length)) as! [SYEmoticon]
            tempArr.append(subArray)
        }
        return tempArr
    }
    
    /**
     通过表情字符串 查找到对应的表情模型
     
     - parameter emoticonStr: 表情字符串
     
     - returns: 表情模型
     */
    func emoticonWithEmoticonStr(emoticonStr: String) -> SYEmoticon? {
        for value in defaultEmoticons {
            // 先从默认表情数组中查找
            if value.chs == emoticonStr {
                return value
            }
        }
        
        for value in lxhEmoticons {
            if value.chs == emoticonStr {
                return value
            }
        }
            return nil
    }
}
